﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace SettlersEngine
{
    internal class AutoPositionTranformable : ContentControl, IVisualObject
    {
        private TranslateTransform m_Transform = new TranslateTransform();
        private ScaleTransform m_Scale = new ScaleTransform();
        public Exported.GameInstance Game { get; private set; }

        public void Scale(Double inScale)
        {
            m_Scale.ScaleX = inScale;
            m_Scale.ScaleY = inScale;
        }

        public AutoPositionTranformable(Exported.GameInstance inInstace, IPositionProvider inPositionable)
        {
            if (inInstace == null)
                throw new ArgumentNullException();

            VerticalAlignment = System.Windows.VerticalAlignment.Top;
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            Game = inInstace;

            // group with world transform
            TransformGroup group = new TransformGroup();

            group.Children.Add(m_Transform);
            group.Children.Add(m_Scale);
            group.Children.Add(Game.WorldTransform);

            RenderTransform = group;

            // initialize position
            Movable_OnPositionChanged(inPositionable, inPositionable.Position, inPositionable.Position);

            // register change handlers
            inPositionable.OnPositionChanged += Movable_OnPositionChanged;

            Game.m_GameRenderPanel.Children.Add(this);
        }

        internal void Movable_OnPositionChanged(IPositionTracker inSender, CyclePoint inOldValue, CyclePoint inNewValue)
        {
            IPositionProvider provider = inSender as IPositionProvider;

            if((provider.UserContext != null) && (provider is IBuildableObject))
                Canvas.SetZIndex(this, (int)((inNewValue.Y + ((IBuildableObject)provider).LockedBounds.Top) * 10));
            else
                Canvas.SetZIndex(this, (int)(inNewValue.Y * 10));

            m_Transform.X = inNewValue.X * Exported.GameInstance.POSITION_SCALE;
            m_Transform.Y = inNewValue.Y * Exported.GameInstance.POSITION_SCALE;
        }

        public void Dispose()
        {
            Game.m_GameRenderPanel.Children.Remove(this);
        }

        public void Hide()
        {
            Visibility = System.Windows.Visibility.Hidden;
        }

        public void Show()
        {
            Visibility = System.Windows.Visibility.Visible;
        }
    }
}
